<template>
  <div class="grid" style="padding-bottom: 20px;">
    <div class="w1200">
      <UserInfo />
      <div class="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="c-01">
              <div class="icon">
                <span>提现金额</span>
              </div>
            </div>
            <div class="c-02">
              <el-form ref="from" :model="form" :rules="rules">
                <el-form-item prop="amount">
                  <el-input type="number" id="topUpCredit-input" min="1"
                    :placeholder="`可提现${getUserInfo.pointsBalance}元`" v-model="form.amount" style="width: 300px;">
                  </el-input>
                </el-form-item>
                <el-form-item prop="account">
                  <el-input type="input" id="topUpCredit-input" placeholder="*待提现支付宝账号" v-model="form.account"
                    style="width: 300px;">
                  </el-input>
                </el-form-item>
                <el-form-item prop="phone">
                  <el-input type="input" id="topUpCredit-input" placeholder="*微信绑定的手机号" v-model="form.phone"
                    style="width: 300px;">
                  </el-input>
                </el-form-item>
                <el-form-item prop="desc">
                  <el-input type="textarea" id="topUpCredit-input" min="1" placeholder="*请描述提现原因（不少于10个字）"
                    v-model="form.desc" rows="4">
                  </el-input>
                </el-form-item>
                <el-form-item prop="code">
                  <div style="display: flex; justify-content: flex-start">
                    <el-input v-model="form.code" autocomplete="off" placeholder="验证码" style="width: 300px;"></el-input>
                    <div class="vercode-mask">
                      <img v-show="imgShow" :src="vercodeImgSrc" @click="refVercode()" />
                      <div class="vercode-mask" v-show="isOverdue" @click="refVercode()">
                        已过期 请刷新
                      </div>
                    </div>
                  </div>
                </el-form-item>
                <!-- <el-form-item prop="name" :label="$t('支付宝账户实名')" v-if="isAli">
                  <el-input type="input" id="topUpCredit-input" :placeholder="$t('请输入支付宝账户实名')" v-model="form.name">
                  </el-input>
                </el-form-item> -->
              </el-form>
            </div>
            <div class="c-03" style="font-size: 14px;color: red;">注：仅支持提现至支付宝，后台审核通过后管理员操作提现。</div>
            <div class="c-04" @click="submit">确认提现</div>
          </el-col>
          <el-col :span="12">
            <div class="img">
              <img src="@/assets/images/tx_03.jpg" alt="">
            </div>
          </el-col>
        </el-row>
      </div>
      <div class="block" style="padding: 40px 0 10px;">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50, 200]" :page-size="pageSize" :total="total"
          layout="prev, pager, next, jumper, sizes" style="text-align: center">
        </el-pagination>
      </div>
      <div>
        <el-table :data="list" stripe style="width: 100%">
          <el-table-column prop="id" :label="$t('订单编号')">
          </el-table-column>
          <el-table-column prop="createTime" :label="$t('提现时间')" />
          <el-table-column prop="pointNums" :label="$t('提现金额')" />
          <el-table-column prop="description" :label="$t('订单备注')">
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import UserInfo from "@/components/home/UserInfo.vue";
import apiService from '@/api/apiService';

export default {
  // eslint-disable-next-line
  name: "Withdrawal",
  data() {
    return {
      visible: false,
      radio: 0,
      agreeCheck: false,
      isAli: true,
      form: {
        amount: '',
        account: '',
        name: '',
        code: ''
      },
      apiRes: {}, // 接口返回数据包
      rules: {
        amount: [
          { required: true, message: this.$t('金额不能为空'), trigger: 'blur' },
          { validator: this.validateAmount, trigger: 'blur' }
        ],
        account: [
          { required: true, message: this.$t('支付宝账户不能为空'), trigger: 'blur' },
        ],
        // name: [
        //   { required: true, message: this.$t('支付宝账户实名不能为空'), trigger: 'blur' },
        // ]
      },
      total: 0,
      currentPage: 1,
      pageSize: 10,
      list: [],
      vercodeImgSrc: "", // 验证码图片
      vercodeToken: "", // 验证码验证token
      isOverdue: false,
      imgShow: false,
    };
  },
  components: {
    UserInfo
  },
  computed: {
    ...mapGetters(["getUserInfo"]),
  },
  mounted() {
    this.getList()
    this.refVercode();
  },
  methods: {
    getList() {
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize,
        type: 2
      }
      apiService.post('point-log', params).then(res => {
        this.total = res.data.total
        this.list = res.data.dataList
      })
    },
    changeType(value) {
      this.resetForm()
      if (value === 1) {
        this.isAli = false

      } else {
        this.isAli = true
      }
    },
    resetForm() {
      this.form.amount = 100
      this.form.account = ''
      this.form.name = ''
    },
    submit() {
      this.$refs.from.validate((valid) => {
        if (!valid) {
          return false;
        }
        // const req = {
        //   account: this.form.account,
        //   amount: this.form.amount,
        //   accountName: this.form.name,
        //   type: this.radio
        // }
        const params = {
          "worderData": `提现金额：${this.form.amount} /n 支付宝账号：${this.form.account} /n 微信绑定的手机号：${this.form.phone} /n 提现原因：${this.form.desc}`,
          "worderType": "提现",
          code: this.form.code,
        }
        console.log(params)
        apiService.post('/add-work', params).then(res => {
          if (res.code === 0) {
            this.$message.success(res.data)
            this.getList()
          } else {
            this.$message.error(res.msg);
          }
        })
      });
    },
    validateAmount(rule, value, callback) {
      value = `${value}`.trim()
      let n = Number(value)
      if (isNaN(n)) {
        return callback(new Error('请输入数字'))
      }
      if (n === 0) {
        return callback()
      }
      // 可见小数位
      let { message } = rule
      const reg = new RegExp(`^[1-9]\\d*$`)
      if (value && !reg.test(value)) {
        callback(new Error(message || '只允许输入正整数'))
      } else if (value < 100) {
        callback(new Error('最低提现金额100'))
      } else {
        return callback()
      }
    },
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },
    refVercode() {
      const that = this;
      apiService.get("/code").then((res) => {
        that.vercodeImgSrc = res.data.imageBase64Data;
        that.vercodeToken = res.data.vercodeToken;
        this.imgShow = true;
        this.isOverdue = false;
        if (this.timer) clearInterval(this.timer);
        this.timer = setInterval(() => {
          res.data.expireTime--;
          if (res.data.expireTime <= 0) {
            that.isOverdue = true;
            this.imgShow = false;
            clearInterval(this.timer);
          }
        }, 1000);
      });
    },

  },
};
</script>

<style lang="scss" scoped>
.top {
  padding-top: 20px;

  .img {
    padding: 20px;

    img {
      width: 100%;
    }
  }

  .c-04 {
    width: 300px;
    height: 42px;
    text-align: center;
    line-height: 42px;
    color: #fff;
    margin-top: 20px;
    background-image: linear-gradient(to right, #5288e8, #105be0);
    cursor: pointer;
  }

  .c-02 {
    padding-top: 20px;
  }

  .c-01 {
    width: 120px;

    .icon {
      span {
        padding-left: 10px;
      }
    }
  }
}

.icon {
  height: 40px;
  background-image: url('@/assets/images/icon_01_03.jpg');
  background-repeat: no-repeat;
  background-position: left;
  background-size: auto 40px;
  padding-left: 30px;

  span {
    color: #fff;
    background-color: #5288e8;
    display: block;
    font-size: 14px;
    border-radius: 0 10px 10px 0;
    display: block;
    height: 40px;
    line-height: 40px;
  }
}

.pay-panel {
  box-shadow: 0 0 0 .2rem #00000005;
  border-radius: 10px;
  border: solid white 1px;
  background: rgb(255, 255, 255);
  margin: auto;
  width: 95%;
}

.shadow-2 {
  margin: auto;
  width: 90%;
  padding: 20px 20px 20px 20px;
}

.text-c-101 {
  width: 100%;
  text-align: left;
  font-weight: bolder;
  margin-bottom: 10px;
}

.text-c-102 {
  width: 100%;
  text-align: left;
  font-weight: bolder;
  margin-bottom: 2px;
}
</style>
